package com.qst.system.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qst.common.annotation.Log;
import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.enums.BusinessType;
import com.qst.system.domain.Examine;
import com.qst.system.service.IExamineService;
import com.qst.system.service.ILoansService;
import com.qst.common.utils.poi.ExcelUtil;
import com.qst.common.core.page.TableDataInfo;

/**
 * 审批Controller
 * 
 * @author 86182
 * @date 2025-08-23
 */
@RestController
@RequestMapping("/system/examine")
public class ExamineController extends BaseController
{
    @Autowired
    private IExamineService examineService;
    
    @Autowired
    private ILoansService loansService;

    /**
     * 查询审批列表
     */
    @PreAuthorize("@ss.hasPermi('system:examine:list')")
    @GetMapping("/list")
    public TableDataInfo list(Examine examine)
    {
        startPage();
        List<Examine> list = examineService.selectExamineList(examine);
        return getDataTable(list);
    }

    /**
     * 导出审批列表
     */
    @PreAuthorize("@ss.hasPermi('system:examine:export')")
    @Log(title = "审批", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Examine examine)
    {
        List<Examine> list = examineService.selectExamineList(examine);
        ExcelUtil<Examine> util = new ExcelUtil<Examine>(Examine.class);
        return util.exportExcel(list, "审批数据");
    }

    /**
     * 获取审批详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:examine:query')")
    @GetMapping(value = "/{exaId}")
    public AjaxResult getInfo(@PathVariable("exaId") Long exaId)
    {
        return AjaxResult.success(examineService.selectExamineById(exaId));
    }

    /**
     * 新增审批
     */
    @PreAuthorize("@ss.hasPermi('system:examine:add')")
    @Log(title = "审批", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Examine examine)
    {
        return toAjax(examineService.insertExamine(examine));
    }

    /**
     * 平台审批通过
     */
    @PreAuthorize("@ss.hasPermi('system:examine:add')")
    @Log(title = "审批", businessType = BusinessType.INSERT)
    @PostMapping("/no")
    public AjaxResult addNoExamine(@RequestBody Examine examine)
    {
        // 审批驳回时同时更新贷款状态为"平台已驳回"(2)
        if (examine.getExaLoaId() != null) {
            loansService.updateLoansState(examine.getExaLoaId(), "2");
        }
        return toAjax(examineService.insertExamine(examine));
    }

    /**
     * 银行审批通过
     */
    @PreAuthorize("@ss.hasPermi('system:examine:add')")
    @Log(title = "审批", businessType = BusinessType.INSERT)
    @PostMapping("/bank")
    public AjaxResult addBankExamine(@RequestBody Examine examine)
    {
        // 银行审批通过时同时更新贷款状态为"银行已审核"(3)
        if (examine.getExaLoaId() != null) {
            loansService.updateLoansState(examine.getExaLoaId(), "3");
        }
        return toAjax(examineService.insertExamine(examine));
    }

    /**
     * 银行审批驳回
     */
    @PreAuthorize("@ss.hasPermi('system:examine:add')")
    @Log(title = "审批", businessType = BusinessType.INSERT)
    @PostMapping("/bankNo")
    public AjaxResult addBankNoExamine(@RequestBody Examine examine)
    {
        // 银行审批驳回时同时更新贷款状态为"银行驳回"(4)
        if (examine.getExaLoaId() != null) {
            loansService.updateLoansState(examine.getExaLoaId(), "4");
        }
        return toAjax(examineService.insertExamine(examine));
    }

    /**
     * 修改审批
     */
    @PreAuthorize("@ss.hasPermi('system:examine:edit')")
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Examine examine)
    {
        return toAjax(examineService.updateExamine(examine));
    }

    /**
     * 删除审批
     */
    @PreAuthorize("@ss.hasPermi('system:examine:remove')")
    @Log(title = "审批", businessType = BusinessType.DELETE)
    @DeleteMapping("/{exaIds}")
    public AjaxResult remove(@PathVariable Long[] exaIds)
    {
        return toAjax(examineService.deleteExamineByIds(exaIds));
    }
}